<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
    private $registry;
    
    protected function _initView()
    {
        $view = new Zend_View();
        $view->doctype('XHTML1_STRICT');
        return $view;
    }
    
    protected function _initInit(){
        $this->registry = Zend_Registry::getInstance();
    }
    
    protected function _initConfigs(){
        $conf_logic = new Zend_Config_Ini(
            APPLICATION_PATH . '/configs/logic.ini'
        );
        Zend_Registry::set('conf_logic', $conf_logic);
        //var_dump(Application_Model_PathsService::getInstance()->get("temp", true));
    }
    
    protected function _initRoles(){
        $acl = new Zend_Acl();
        
        $acl->addRole(new Zend_Acl_Role('admin'))
        ->addRole(new Zend_Acl_Role('copywriter'))
        ->addRole(new Zend_Acl_Role('worker'))
        ->addRole(new Zend_Acl_Role('client'))
        ->addRole(new Zend_Acl_Role('guest'));
        
        $acl->add(new Zend_Acl_Resource('add_news'));
        $acl->add(new Zend_Acl_Resource('edit_news'));
        $acl->add(new Zend_Acl_Resource('remove_news'));
        
        $acl->add(new Zend_Acl_Resource('add_user'));
        $acl->add(new Zend_Acl_Resource('edit_user'));
        $acl->add(new Zend_Acl_Resource('remove_user'));
        $acl->add(new Zend_Acl_Resource('show_users'));
        $acl->add(new Zend_Acl_Resource('add_system'));
        
        $acl->add(new Zend_Acl_Resource('show_costs'));
        
        $acl->add(new Zend_Acl_Resource('add_handly_spec'));
        $acl->add(new Zend_Acl_Resource('remove_handly_specs'));
        $acl->add(new Zend_Acl_Resource('edit_handly_specs'));
        
        $acl->add(new Zend_Acl_Resource('add_fill'));
        $acl->add(new Zend_Acl_Resource('remove_fill'));
        $acl->add(new Zend_Acl_Resource('edit_fill'));
        
        $acl->add(new Zend_Acl_Resource('show_office'));
        
        $acl->add(new Zend_Acl_Resource('remove_system'));
        $acl->add(new Zend_Acl_Resource('edit_system'));
        
        $acl->add(new Zend_Acl_Resource('unlogin'));
        
        $acl->allow(array('admin'), 'add_user');
        $acl->allow(array('admin'), 'edit_user');
        $acl->allow(array('admin'), 'remove_user');
        $acl->allow(array('admin'), 'show_users');
        
        $acl->allow(array('admin', 'copywriter'), 'add_news');
        $acl->allow(array('admin', 'copywriter'), 'edit_news');
        $acl->allow(array('admin', 'copywriter'), 'remove_news');
        $acl->allow(array('admin', 'copywriter', 'worker', 'client'), 'show_office');
        
        $acl->allow(array('admin'), 'add_handly_spec');
        $acl->allow(array('admin'), 'remove_handly_specs');
        $acl->allow(array('admin'), 'edit_handly_specs');
        $acl->allow(array('admin'), 'show_costs');
        
        $acl->allow(array('admin'), 'add_system');
        $acl->allow(array('admin'), 'remove_system');
        $acl->allow(array('admin'), 'edit_system');
        
        $acl->allow(array('admin'), 'add_fill');
        $acl->allow(array('admin'), 'remove_fill');
        $acl->allow(array('admin'), 'edit_fill');
        
        $acl->allow(array('admin'), 'unlogin');
        $acl->allow(array('copywriter'), 'unlogin');
        $acl->allow(array('worker'), 'unlogin');
        $acl->allow(array('client'), 'unlogin');
        
        $this->registry->acl = $acl;
        //Zend_Debug::dump($acl);
    }
	
    protected function _initDatabase(){
        $options = $this->getOptions();
        $dbAdapter = new Zend_Db_Adapter_Pdo_Mysql($options['database']['params']);
		$dbAdapter->query('SET NAMES utf8');
        Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
		$this->registry->dbAdapter = $dbAdapter;
    }
    
	protected function _initCurrentUser() {
		$uni_session_code = @$_COOKIE['cold'];
        $currentUser = null;
        $usersService = Application_Model_UsersService::getInstance();
        if (isset( $uni_session_code ) && strlen($uni_session_code)>0){
            $currentUser = $usersService->search_by_uni_session_code($uni_session_code);
        }
        if ($currentUser == null){
            $currentUser = new Application_Model_User();
            $currentUser->set("role", "guest");
            setcookie("cold", "" , 0 , '/');
        } else {
            $currentUser->set('date_last_entry', time());
            $usersService->save( $currentUser );
            setcookie("cold", $uni_session_code , time()+3600, '/');
        }
        $this->registry->currentUser = $currentUser;
	}
    
    
	
	protected function _initNavigation(){
		$this->bootstrap('layout');
		$layout = $this->getResource('layout');
		$view = $layout->getView();
		$navigation = new Zend_Navigation(array(
			array(
				'controller' => 'index',
				'action' => 'index',
				'label' => 'Новини',
				'id' => 'menu_news'
			),
			array(
				'controller' => 'albums',
				'action' => 'index',
				'label' => 'Портфоліо',
				'id' => 'menu_portfolio'
			),
			array(
				'controller' => 'calculator',
				'action' => 'index',
				'label' => 'Калькулятор',
				'id' => 'menu_calculator'
			),
			array(
				'controller' => 'materials',
				'action' => 'showfillprices',
				'label' => 'Матеріали',
				'id' => 'menu_prices',
                'pages'=>array(
                    array(
						'controller' => 'materials',
						'action' => 'showfillprices',
						'label' => 'Заповнення',
						'id' => 'menu_show_fill_prices'
					),
                    array(
						'controller' => 'materials',
						'action' => 'showsystemprices',
						'label' => 'Системи',
						'id' => 'menu_show_prices_systems'
					),
					array(
						'controller' => 'materials',
						'action' => 'showhandlyspecs',
						'label' => 'Типи ручок',
						'id' => 'menu_show_handly_specs'
					)
				)
			),
			array(
				'controller' => 'staticpages',
				'action' => 'contacts',
				'label' => 'Контакти',
				'id' => 'menu_contacts'
			),
            array(
				'controller' => 'office',
				'action' => 'index',
				'label' => 'Офіс',
				'id' => 'menu_about',
				'pages'=>array(
					array(
						'controller' => 'index',
						'action' => 'add',
						'label' => 'Додати новину',
						'id' => 'menu_add_news',
                        'res' => 'add_news'
					),
					array(
						'controller' => 'users',
						'action' => 'add',
						'label' => 'Додати користувача',
						'id' => 'menu_add_user',
                        'res' => 'add_user'
					),
                    array(
						'controller' => 'users',
						'action' => 'index',
						'label' => 'Всі користувачі',
						'id' => 'menu_show_users',
                        'res' => 'show_users'
					),
                    array(
						'controller' => 'materials',
						'action' => 'addhandlyspec',
						'label' => 'Додати ручку',
						'id' => 'menu_add_handly_spec',
                        'res' => 'add_handly_spec'
					),
                    array(
						'controller' => 'materials',
						'action' => 'addsystem',
						'label' => 'Додати систему',
						'id' => 'menu_add_system',
                        'res' => 'add_system'
					),
                    array(
						'controller' => 'materials',
						'action' => 'addfillprice',
						'label' => 'Додати заповнення',
						'id' => 'menu_add_fill_price',
                        'res' => 'add_fill'
					)
				)
			),
			array(
				'controller' => 'staticpages',
				'action' => 'about',
				'label' => 'Про нас',
				'id' => 'menu_about'
			)
		));
		$view->navigation($navigation);

		//$view->navigation()->breadcrumbs()->setPartial('_breadcrumbs.phtml');

	}
}